import 'package:amway_superapp_creator_plugin/config/colors_config.dart';
import 'package:amway_superapp_creator_plugin/routers/application.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_screenutil/screenutil.dart';

class CommonWebView extends StatefulWidget {
  String url;

  CommonWebView(this.url);

  @override
  _CommonWebViewState createState() => _CommonWebViewState();
}

class _CommonWebViewState extends State<CommonWebView> {
  InAppWebViewController webView;
  String _title = "详情";

  String url = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        centerTitle: true,
        leading: IconButton(
          onPressed: () {
            Application.router.pop(context);
          },
          icon: Icon(
            Icons.arrow_back_ios,
            color: Colors.black,
          ),
        ),
        title: Text(
          _title,
          style: TextStyle(
              fontSize: ScreenUtil().setSp(16), color: ColorsConfig.c_2c2c2c),
        ),
      ),
      body: Container(
        child: InAppWebView(
          initialUrl: widget.url,
          initialOptions: InAppWebViewGroupOptions(
              crossPlatform: InAppWebViewOptions(
                debuggingEnabled: true,
                useShouldOverrideUrlLoading: true,
              ),
              android: AndroidInAppWebViewOptions(

              )
          ),
          onWebViewCreated: (InAppWebViewController controller) {
            webView = controller;
            print("onWebViewCreated");
          },
          onLoadStart: (InAppWebViewController controller, String url) {
            print("onLoadStart $url");
            setState(() {
              widget.url = url;
            });
          },
          shouldOverrideUrlLoading: (controller, shouldOverrideUrlLoadingRequest) async {
            var url = shouldOverrideUrlLoadingRequest.url;
            var uri = Uri.parse(url);

            if (!["http", "https", "file",
              "chrome", "data", "javascript",
              "about"].contains(uri.scheme)) {

            }

            return ShouldOverrideUrlLoadingAction.ALLOW;
          },

          onLoadStop: (InAppWebViewController controller, String url) async {
            print("onLoadStop $url");

          },
          onProgressChanged: (InAppWebViewController controller, int progress) {

          },
          onReceivedServerTrustAuthRequest: (InAppWebViewController controller, ServerTrustChallenge challenge) async {
            return ServerTrustAuthResponse(action: ServerTrustAuthResponseAction.PROCEED);
          },
        ),
      ),
    );
  }
}


